// pages/jieQi/jieQi.js
const log = require('../log/log.js')
const util = require('../utils/util.js')
const sizhu = require('../sizhu/sizhu.js')
//十二个地支       0     1     2     3    4     5     6     7     8     9    10   11
const zhiList = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"]
//节令
const jieLingList = ['大雪', '小寒', '立春', '惊蛰', '清明', '立夏', '芒种', '小暑', '立秋', '白露', '寒露', '立冬']
Page({

    /**
     * 页面的初始数据
     */
    data: {
        scrHeight: 600
    },
    onShareAppMessage: function () {
        return {
            title: '分享六爻策给你的伙伴吧',
            path: '/pages/index/index',
            imageUrl: '/images/6YCSHARE.png'
        }
    },
    autoStyle: function () {
        var systeminfo = wx.getSystemInfoSync();
        log.info('index-autoStyle-获取系统参数', systeminfo)
        console.log(systeminfo);

        //px为单位
        var sW = systeminfo.screenWidth;
        var sH = systeminfo.screenHeight

        var rate = 750 / sW;
        console.log(rate);

        //rpx为单位
        var rW = parseInt(sW * rate)
        var rH = parseInt(sH * rate)

        //屏幕长宽比
        var hwNum = sH / sW

        //pad的参数
        var pam = 1
        if (hwNum < 1.4) {
            pam = 0.72
        }

        var scrHeight = parseInt(rH * 0.65)

        this.setData({
            scrHeight: scrHeight,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

        this.autoStyle()

        var date = new Date()
        var year = date.getFullYear()
        this.showJieQi(year)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },
    getInputValue: function (e) {
        this.setData({
            queryInput: e.detail.value
        })
    },
    query: function () {
        var inputVal = this.data.queryInput

        if (!inputVal) {
            var date = new Date()
            var year = date.getFullYear()
            this.showJieQi(year)
        }

        var reg = /^[0-9]+$/;
        if (inputVal && !reg.test(inputVal)) {
            wx.showToast({
                title: '请输入年份',
                icon: 'none',
                duration: 1500
            })
            return;
        } else if (inputVal && reg.test(inputVal) && (inputVal < 500 || inputVal > 5000)) {
            wx.showToast({
                title: '年份范围500 - 5000',
                icon: 'none',
                duration: 1500
            })
            return;
        } else if (inputVal) {
            var year = parseInt(inputVal)

            this.showJieQi(year)
        }
    },
    showJieQi: function (year) {
        var jieQi1 = sizhu.getJieQi(year)
        var jieQi2 = sizhu.getJieQi(year + 1)
        var jieQi = jieQi1.concat(jieQi2)

        var jieQiList = []
        for (var i = 0; i < jieQi.length; i++) {
            var obj = new Object()
            obj.jieQi = jieQi[i].jieQi
            obj.date = util.formatTime(jieQi[i].date)
            if (jieLingList.indexOf(obj.jieQi) > - 1) {
                obj.diZhi = zhiList[jieLingList.indexOf(obj.jieQi)]
            }
            jieQiList.push(obj)
        }
        this.setData({
            jieQiList: jieQiList
        })
    },
    adLoad() {
        log.info('小程序视频广告加载成功')
        log.addFilterMsg('adLoad-JQ')
    },
    adError(err) {
        log.info('小程序视频广告加载失败', err)
        log.addFilterMsg('adError-JQ')
    },
    adClose(){
        log.info('小程序视频广告关闭')
        log.addFilterMsg('adClose-JQ')
    }
})